<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

/**
 * 本地存储的账户信息
 *
 * @type {any}
 */
const account = JSON.parse(localStorage.getItem('account-info') || '{}')

/**
 * 是否是修改的判断
 *
 * @type {boolean}
 */
let isEdit = false

/**
 * 学院数据
 *
 * @type {UnwrapNestedRefs<{total: number, collageName: string, form: {collageUpdateTime: string, collageName: string, collageCreateTime: string, collageCreateMan: string, collageNo: string, collageUpdateMan: string}, formVisible: boolean, pageSize: number, tableData: *[], pageNum: number}>}
 */
const collageData = reactive({
    collageName: '',
    tableData: [],
    total: 0,
    pageSize: 6,
    pageNum: 1,
    formVisible: false, // 是否显示弹窗
    form: {
        collageNo: '',
        collageName: '',
        collageCreateMan: '',
        collageCreateTime: '',
        collageUpdateMan: '',
        collageUpdateTime: '',
    }
})

/**
 * 加载学院
 */
const loadCollage = () => {
    request.get(`/api/collage/find-collage-page`, {
        params: {
            pageNum: collageData.pageNum,
            pageSize: collageData.pageSize,
            collageName: collageData.collageName,
        }
    }).then(res => {
        collageData.tableData = res.data?.list || []
        collageData.total = res.data?.list.map(item => item.collageCountNumber)[0] || 0;
    })
}
// 调用学院加载方法，获取后台数据
loadCollage()

/**
 * 翻页
 */
const handelCurrentChange = () => {
    // 翻页时，重新加载数据即可
    loadCollage()
}

/**
 * 重置
 */
const reset = () => {
    collageData.collageName = ''
    // 重置时，重新加载数据即可
    loadCollage()
}

/**
 * 新增学院
 */
const addCollage = () => {
    isEdit = false
    // 清空上次残留的数据
    collageData.form = {}
    collageData.formVisible = true
}

/**
 * 提交新增的课程
 */
const submitAddCollage = () => {
    if (account.accountRoleName === 'visitor-admin') {
        ElMessage.info("游客-管理员仅可以查看信息")
    } else {
        collageData.form.collageCreateMan = account.accountNo
        request.post(`/api/collage/add-collage`, collageData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadCollage()
                // 关闭弹窗
                collageData.formVisible = false
                ElMessage.success("新增成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}

/**
 * 修改学院
 *
 * @param row 按行修改
 */
const editCollage = (row) => {
    isEdit = true
    collageData.form = JSON.parse(JSON.stringify(row))
    collageData.formVisible = true
}

/**
 * 提交修改的学院
 */
const submitEditCollage = () => {
    if (account.accountRoleName === 'visitor-admin') {
        ElMessage.info("游客-管理员仅可以查看信息")
    } else {
        collageData.form.collageUpdateMan = account.accountNo
        request.put(`/api/collage/update-collage`, collageData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadCollage()
                // 关闭弹窗
                collageData.formVisible = false
                ElMessage.success("编辑成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}

/**
 * 删除学院
 *
 * @param collageNo 学院号
 */
const deleteCollage = (collageNo) => {
    ElMessageBox.confirm("删除数据后无法恢复，您确认删除此条数据？", "删除确认", {type: 'warning'}).then(() => {
        if (account.accountRoleName === 'visitor-admin') {
            ElMessage.info("游客-管理员仅可以查看信息")
        } else {
            request.delete(`/api/collage/delete-collage/${collageNo}`).then(res => {
                if (res.code === "200") {
                    // 重新获取数据
                    loadCollage()
                    ElMessage.success("删除成功")
                } else {
                    ElMessage.error(res.msg)
                }
            })
        }
    }).catch(() => {
        ElMessage({
            type: "info",
            message: "取消删除",
        })
    })
}
</script>

<template>
    <div v-if="account.accountRoleName === 'system-admin' || account.accountRoleName === 'visitor-admin'">
        <div class="card" style="margin-bottom: 10px;">
            <el-input style="width: 300px; margin-right: 10px;" v-model="collageData.collageName"
                      placeholder="请输入学院名称查询"
                      :prefix-icon="Search"/>
            <el-button @click="loadCollage" type="primary" style="margin-left: 10px;">查询</el-button>
            <el-button @click="reset" type="info">重置</el-button>
        </div>
        <div class="card" style="margin-bottom: 10px;">
            <div>
                <el-button @click="addCollage" type="primary" style="margin-bottom: 10px;">新增</el-button>
            </div>
            <div>
                <el-table :data="collageData.tableData" style="width: 100%">
                    <el-table-column prop="collageNo" label="学院号"/>
                    <el-table-column prop="collageName" label="学院名"/>
                    <el-table-column prop="collageCreateMan" label="创建人"/>
                    <el-table-column prop="collageCreateTime" label="创建时间"/>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="editCollage(scope.row)" type="primary" size="small" plain>编辑
                            </el-button>
                            <el-button @click="deleteCollage(scope.row.collageNo)" type="danger" size="small"
                                       plain>删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="collageData.pageNum"
                           v-model:page-size="collageData.pageSize"
                           @current-change="handelCurrentChange"
                           background layout="prev, pager, next" :total="collageData.total"/>
        </div>

        <el-dialog v-model="collageData.formVisible" :close-on-click-modal="false" title="学院信息">
            <el-form :model="collageData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;" v-if="isEdit === false">
                <el-form-item label="学院号">
                    <el-input placeholder="请输入学院号" v-model="collageData.form.collageNo"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学院名">
                    <el-input placeholder="请输入学院名" v-model="collageData.form.collageName" autocomplete="off"
                              :maxlength="20"></el-input>
                </el-form-item>
            </el-form>

            <el-form :model="collageData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;" v-if="isEdit === true">
                <el-form-item label="学院号">
                    <el-input placeholder="请输入学院号" v-model="collageData.form.collageNo"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="学院名">
                    <el-input placeholder="请输入学院名" v-model="collageData.form.collageName" autocomplete="off"
                              :maxlength="20"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="collageData.formVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitAddCollage" v-if="isEdit === false">提交</el-button>
                    <el-button type="primary" @click="submitEditCollage" v-if="isEdit === true">提交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>

</style>